package com.learn.servloader.switches;

import com.learn.servloader.Parser;
import com.learn.servloader.TypeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * 耦合性相对比较强，代码改动比较多，还不如基于Map的自动注入策略模式
 */
@Service
public class ParseClient {

    private Parser csvParser, jsonParser;
    private Reader reader;

    @Autowired
    public ParseClient(@Qualifier(TypeConstants.CSV_PARSER) Parser csvParser, @Qualifier(TypeConstants.JSON_PARSER) Parser jsonParser) {
        this.csvParser = csvParser;
        this.jsonParser = jsonParser;
    }

    public void setReader(Reader reader) {
        this.reader = reader;
    }

    public List getAll(ContentType contentType) {

        switch (contentType) {
            case CSV:
                return csvParser.parse(reader);
            case JSON:
                return jsonParser.parse(reader);
        }

        return new ArrayList<>();
    }

}